<?php
/*
 * @package     submitearticles
 * @author      Apul Gupta<apulmca2k4@gmail.com>
 * @description Checks for all spamming in value
 */

class sfSpamValidator extends sfValidatorBase
{
    protected static $articleConfig;
    protected function configure($options = array(), $messages = array())
    {
        self::$articleConfig = sfConfig::get('app_validation_article');
        /* Setting Default values */
        $this->addOption('max_links',        self::$articleConfig['links']['count']);
        $this->addOption('bold_tag',         self::$articleConfig['bold']['count']);
        $this->addOption('italic_tag',       self::$articleConfig['italic']['count']);
        $this->addOption('script_tag',       self::$articleConfig['script']['value']);
        $this->addOption('font_tag',         self::$articleConfig['font']['value']);
        $this->addOption('min_words',        self::$articleConfig['words']['min']);
        $this->addOption('max_words',        self::$articleConfig['words']['max']);


        /* Setting Default Error messages */
        $this->addMessage('max_links_error',  self::$articleConfig['links']['error']);
        $this->addMessage('bold_tag_error',   self::$articleConfig['bold']['error']);
        $this->addMessage('italic_tag_error', self::$articleConfig['italic']['error']);
        $this->addMessage('script_tag_error', self::$articleConfig['script']['error']);
        $this->addMessage('font_tag_error',   self::$articleConfig['font']['error']);
        $this->addMessage('words_error',      self::$articleConfig['words']['error']);

    }
    /**
    * Cleans the input value.
    *
    * @param  mixed $value The input value
    * @return mixed The cleaned value
    * @throws sfValidatorError
    */
    public function doClean($value)
    {
        $regexForLinks = "/".array_fill(0, $this->getOption('max_links_error')+1 ,"http")."/is";

        if(preg_match($regexForLinks,$value))
        {
            throw new sfValidatorError($this, 'max_links_error', array('value' => $value, 'max_links' => $this->getOption('max_links_error')));
        }
        if(MyUtility::countOccurence('b', $value)>$this->getOption('bold_tag'))
        {
            throw new sfValidatorError($this, 'bold_tag_error', array('value' => $value, 'bold_tag' => $this->getOption('bold_tag_error')));
        }
        if(MyUtility::countOccurence('strong', $value)>$this->getOption('bold_tag'))
        {
            throw new sfValidatorError($this, 'bold_tag_error', array('value' => $value, 'bold_tag' => $this->getOption('bold_tag_error')));
        }
        if(MyUtility::countOccurence('i', $value)>$this->getOption('italic_tag'))
        {
            throw new sfValidatorError($this, 'italic_tag_error', array('value' => $value, 'italic_tag' => $this->getOption('italic_tag_error')));
        }
        if($this->getOption('script_tag'))
        {
            if(MyUtility::countOccurence('script', $value)>0)
            {
                throw new sfValidatorError($this, 'script_tag_error', array('value' => $value, 'script_tag' => $this->getOption('script_tag_error')));
            }
        }
        if($this->getOption('font_tag'))
        {
            if(MyUtility::countOccurence('font', $value)>0)
            {
                throw new sfValidatorError($this, 'font_tag_error', array('value' => $value, 'font_tag' => $this->getOption('font_tag_error')));
            }
        }
        $words = str_word_count($value, 0);
        if($words > $this->getOption('max_words') || $words < $this->getOption('min_words') )
        {
            throw new sfValidatorError($this, 'words_error', array('value' => $value, 'words_error' => $this->getOption('words_error')));
        }

        return $value;
    }
}
?>